<?php

namespace app\index\logic;

use app\common\exception\LogicException;
use app\common\logic\EncryptLogic;
use app\common\logic\PhoneCodeLogic;
use app\common\logic\RandCreateStrLogic;
use app\model\User;

class UserLogic
{
    /**
     * @Description 注册
     * @author: zj
     * @param string $name
     * @param string $phone
     * @param string $password
     * @param string $phoneCode
     * @param string $company
     * @param string $province
     * @param string $city
     * @param string $district
     * @param string $address
     * @time 2024/03/18 17:46:57
     * @return int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function register(string $name,string $phone,string $password,string $phoneCode,string $company,string $province,string $city,string $district,string $address)
    {
        // 判断手机验证码
//        $this->checkRegCode($phone,$phoneCode);

        $userModel = new User();
        // 判断用户名
        $exist = $userModel->checkNameIsExist($name);
        if ($exist) {
            throw new LogicException("用户名已存在");
        }

        // 判断手机号
        $exist = $userModel->checkPhoneIsExist($phone);
        if ($exist) {
            throw new LogicException("手机号已注册");
        }

        // 获取密码盐
        $randLogic = new RandCreateStrLogic();
        $salt = $randLogic->create(4);

        // 加密
        $encryptLogic = new EncryptLogic();
        $pwd = $encryptLogic->encryptMD5($password,$salt);

        return $userModel->insertGetId([
            'name' => $name,
            'phone' => $phone,
            'password' => $pwd,
            'salt' => $salt,
            'status' => 1,
            'company' => $company,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'address' => $address,
            'create_time' => request()->time(),
            'update_time' => request()->time(),
        ]);
    }

    /**
     * @Description 检验注册验证码
     * @author: zj
     * @param string $phone
     * @param string $code
     * @time 2023/08/29 11:44:46
     * @return true
     */
    public function checkRegCode(string $phone,string $code)
    {
        $logic = new PhoneCodeLogic();
        $logic->checkCode($phone,$code);

        return true;
    }

    /**
     * @Description 获取手机验证码
     * @author: zj
     * @param string $phone
     * @param string $code
     * @time 2023/08/29 14:29:53
     * @return true
     */
    public function getRegPhoneCode(string $phone, string $code)
    {
        $logic = new PhoneCodeLogic();
//        $logic->getCode($phone, $code, AliSmsService::VERIFICATION_CODE);

        return true;
    }
}